﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.Services;

public partial class Home : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();
    DataSet dsImages = new DataSet();
    string cnt = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {           
            //ListBind2();
            DataList2.DataSource = GetCustomersData(1);
            DataList2.DataBind();
           
        }
    }
    public static DataSet GetCustomersData(int pageIndex)
    {
        string query = "[GetGroupsPageWise]";
        SqlCommand cmd = new SqlCommand(query);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
        cmd.Parameters.AddWithValue("@PageSize", 10);
        cmd.Parameters.Add("@PageCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
        return GetData(cmd);
    }
    private static DataSet GetData(SqlCommand cmd)
    {
        string strConnString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds, "tbGroup");
                    DataTable dt = new DataTable("PageCount");
                    dt.Columns.Add("PageCount");
                    dt.Rows.Add();
                    dt.Rows[0][0] = cmd.Parameters["@PageCount"].Value;
                    ds.Tables.Add(dt);
                    return ds;
                }
            }
        }
    }

    [WebMethod]
    public static string GetCustomers(int pageIndex)
    {
        return GetCustomersData(pageIndex).GetXml();
    }
    public string GetSmallContents(string str, int chars)
    {
        if (str.Length > chars)
        {
            str = str.Substring(0, chars);
            str = str + "...";
        }
        return str;
    }
    private void ListBind2()
    {
        string qry = "select * from tbGroup";

        SqlDataAdapter adp = new SqlDataAdapter(qry, cnt);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        DataList2.DataSource = ds;
        DataList2.DataBind();
    }
    protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "GroupDetail")
        {
            Response.Redirect("GroupDetail.aspx?GroupID=" + e.CommandArgument);//         +"& NewsID="+aa);
        }
    }
    protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rptDetails1 = (Repeater)e.Item.FindControl("TopNews1");
         
            if (rptDetails1 != null)
            {
                string query = "select  * from tbItems where groupID=" + DataBinder.Eval(e.Item.DataItem, "GroupID").ToString();
                SqlDataAdapter adp = new SqlDataAdapter(query, cnt);
                DataSet ds = new DataSet();
                adp.Fill(ds);
                //dsImages = SqlHelper.ExecuteDataset(con, CommandType.Text, query);
                if (ds != null)
                {
                    DataRow nRow = ds.Tables[0].NewRow();
                    nRow["ItemThumbPath"] = @"Resources/AddButton.png";                    
                    ds.Tables[0].Rows.Add(nRow);
                    ds.GetChanges();
                  //  gv.DataSource = ds.Tables[0]; 
                    rptDetails1.DataSource = ds;
                    rptDetails1.DataBind();
                }
            }

        }
    }

    protected void linkButton_Item(object sender, EventArgs e)
    {
        var commandArgs = sender as LinkButton;
        var groupID = commandArgs.ClientID;
        string[] words = groupID.Split('_');
        string GroupID=words[2].ToString();
        int ItemID = Convert.ToInt32(words[4])+1;
        string query = "select  * from tbSubItems where ItemID=" + ItemID.ToString();
        SqlDataAdapter adp = new SqlDataAdapter(query, cnt);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            Response.Redirect("ItemChild.aspx?ItemID=" + ItemID);
        }
        //foreach (string word in words)
        //{
        //    var a = word;
        //}
        else
        {
            int a = Convert.ToInt32(GroupID) + 1;
            if (commandArgs.CommandArgument == "")
            {
                Response.Redirect("WebControls/Item.aspx?GroupID=" + a);

            }
            else
            {
                Response.Redirect("LeafItem.aspx?ItemID=" + commandArgs.CommandArgument);

            }
        }
        // 
        

    }
}